'''
This file is part of DopplerShift.

    DopplerShift is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    DopplerShift is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with DopplerShift.  If not, see <http://www.gnu.org/licenses/>.
'''

from txredisapi import RedisFactory,RedisProtocol
from dopplershift.collector.backends.auth.default import DefaultAuthBackend
from txredis.protocol import Redis


class RedisBasedBackend(DefaultAuthBackend, RedisFactory):
    '''
    Redis based backend, need for use async client library for Redis
    '''
    _connected = False 
    protocol = RedisProtocol
    def __init__(self, host='localhost', port=6379, on=True, dbid=0, **kwargs):
        self._host = host
        self._port = int(port)
        self._on = on
        self.dbid = int(dbid)
        #self.pool = []
        RedisFactory.__init__(self, self.dbid, 0)
        #self.configure()
    def _getUserSession(self, result, event_handler):
        res = self.pickler(str(result))
        import json, sys
        self._generate(event_handler, res)
    def removeAuth(self, key):
        return self._proto.delete(key)
    def addConnection(self, conn):
        self._proto = conn
        self._connected = True
    def buildProtocol(self, addr):
        proto = self.protocol()
        proto.factory = self
        return proto
        #RedisFactory.bu
        '''
        '''